/*
 * \file xml_parser.c
 *
 *  Created on: Sep 8, 2012
 *      Author: Adrian Ortega Garcia
 */

#include "xml_parser.h"
#include "logging.h"

static void print_element_names(xmlNode *a_node);

void	parse_document(const char *doc_name)
{
	if(!doc_name) {
		LOG_ERR("Invalid document name.\n");
		return;
	}

	xmlDocPtr	doc;
	xmlNodePtr	current;

	/*
	 * this initialize the library and check potential ABI mismatches
	 * between the version it was compiled for and the actual shared
	 * library used.
	 */
	LIBXML_TEST_VERSION

	doc = xmlParseFile(doc_name);

	if( doc == NULL) {
		LOG_ERR("Document not parsed successfully.\n");
		return;
	}

	current = xmlDocGetRootElement(doc);

	if( current == NULL) {
		LOG_ERR("Empty document.\n");
		xmlFreeDoc(doc);
	}

	if( xmlStrcmp(current->name,(const xmlChar *)"x86reference")){
		LOG_ERR("Wrong document type.\n");
		xmlFreeDoc(doc);
	}
	LOG_INFO("Valid document type.\n");


	xmlFreeDoc(doc);
	xmlCleanupParser();
	return;
}

static void print_element_names(xmlNode * a_node)
{
	xmlNode *cur_node = NULL;

	for (cur_node = a_node; cur_node; cur_node = cur_node->next) {
		if (cur_node->type == XML_ELEMENT_NODE) {
			LOG_INFO("node type: Element, name: %s\n", cur_node->name);
		}

		print_element_names(cur_node->children);
	}
}
